<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>find 命令之 exec | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>find 命令之 exec</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Linux命令/">Linux命令</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;find是我们很常用的一个Linux命令，但是我们一般查找出来的并不仅仅是看看而已，还会有进一步的操作，这个时候exec的作用就显现出来了。</p>
<h2 id="exec解释："><a href="#exec解释：" class="headerlink" title="exec解释："></a>exec解释：</h2><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;-exec  参数后面跟的是command命令，它的终止是以;为结束标志的，所以这句命令后面的分号是不可缺少的，考虑到各个系统中分号会有不同的意义，所以前面加反斜杠。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;{}   花括号代表前面find查找出来的文件名。</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;使用find时，只要把想要的操作写在一个文件里，就可以用exec来配合find查找，很方便的。在有些操作系统中只允许-exec选项执行诸如l s或ls -l这样的命令。大多数用户使用这一选项是为了查找旧文件并删除它们。建议在真正执行rm命令删除文件之前，最好先用ls命令看一下，确认它们是所要删除的文件。 exec选项后面跟随着所要执行的命令或脚本，然后是一对儿{ }，一个空格和一个\，最后是一个分号。为了使用exec选项，必须要同时使用print选项。如果验证一下find命令，会发现该命令只输出从当前路径起的相对路径及文件名。</p>
<h2 id="使用实例"><a href="#使用实例" class="headerlink" title="使用实例"></a>使用实例</h2><h3 id="实例1：ls-l-命令放在-find-命令的-exec-选项中"><a href="#实例1：ls-l-命令放在-find-命令的-exec-选项中" class="headerlink" title="实例1：ls -l 命令放在 find 命令的 -exec 选项中"></a>实例1：ls -l 命令放在 find 命令的 -exec 选项中</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">find . -<span class="built_in">type</span> d -<span class="built_in">exec</span> ls -l &#123;&#125; \;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># find . -type f -exec ls -l &#123;&#125; \; </span></div><div class="line">-rw-r--r-- 1 root root 127 10-28 16:51 ./log2014.log</div><div class="line">-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-2.log</div><div class="line">-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-3.log</div><div class="line">-rw-r--r-- 1 root root 0 10-28 14:47 ./test4/log3-1.log</div><div class="line">-rw-r--r-- 1 root root 33 10-28 16:54 ./log2013.log</div><div class="line">-rw-r--r-- 1 root root 302108 11-03 06:19 ./log2012.log</div><div class="line">-rw-r--r-- 1 root root 25 10-28 17:02 ./log.log</div><div class="line">-rw-r--r-- 1 root root 37 10-28 17:07 ./log.txt</div><div class="line">-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-2.log</div><div class="line">-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-3.log</div><div class="line">-rw-r--r-- 1 root root 0 10-28 14:47 ./test3/log3-1.log</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong><br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;上例中，find 命令匹配到了当前目录下的所有普通文件，并在 -exec 选项中使用 ls -l 命令将他们列出来。</p>
<h3 id="实例2：在目录中查找更改时间在-n-日以前的文件并删除它们"><a href="#实例2：在目录中查找更改时间在-n-日以前的文件并删除它们" class="headerlink" title="实例2：在目录中查找更改时间在 n 日以前的文件并删除它们"></a>实例2：在目录中查找更改时间在 n 日以前的文件并删除它们</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">find . -<span class="built_in">type</span> f -mtime +14 -<span class="built_in">exec</span> rm &#123;&#125; \;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">总计 328</div><div class="line">-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log</div><div class="line">-rw-r--r-- 1 root root     33 10-28 16:54 log2013.log</div><div class="line">-rw-r--r-- 1 root root    127 10-28 16:51 log2014.log</div><div class="line">lrwxrwxrwx 1 root root      7 10-28 15:18 log_link.log -&gt; log.log</div><div class="line">-rw-r--r-- 1 root root     25 10-28 17:02 log.log</div><div class="line">-rw-r--r-- 1 root root     37 10-28 17:07 log.txt</div><div class="line">drwxr-xr-x 6 root root   4096 10-27 01:58 scf</div><div class="line">drwxrwxrwx 2 root root   4096 10-28 14:47 test3</div><div class="line">drwxrwxrwx 2 root root   4096 10-28 14:47 test4</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># find . -type f -mtime +14 -exec rm &#123;&#125; \;</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">总计 312</div><div class="line">-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log</div><div class="line">lrwxrwxrwx 1 root root      7 10-28 15:18 log_link.log -&gt; log.log</div><div class="line">drwxr-xr-x 6 root root   4096 10-27 01:58 scf</div><div class="line">drwxrwxrwx 2 root root   4096 11-12 19:32 test3</div><div class="line">drwxrwxrwx 2 root root   4096 11-12 19:32 test4</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong><br>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在 shell 中用任何方式删除文件之前，应当先查看相应的文件，一定要小心！当使用诸如 mv 或 rm 命令时，可以使用 -exec 选项的安全模式。它将在对每个匹配到的文件进行操作之前提示你。</p>
<h3 id="实例3：在目录中查找更改时间在-n-日以前的文件并删除它们，在删除之前先给出提示"><a href="#实例3：在目录中查找更改时间在-n-日以前的文件并删除它们，在删除之前先给出提示" class="headerlink" title="实例3：在目录中查找更改时间在 n 日以前的文件并删除它们，在删除之前先给出提示"></a>实例3：在目录中查找更改时间在 n 日以前的文件并删除它们，在删除之前先给出提示</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">find . -name <span class="string">"*.log"</span> -mtime +5 -ok rm &#123;&#125; \;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">总计 312</div><div class="line">-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log</div><div class="line">lrwxrwxrwx 1 root root      7 10-28 15:18 log_link.log -&gt; log.log</div><div class="line">drwxr-xr-x 6 root root   4096 10-27 01:58 scf</div><div class="line">drwxrwxrwx 2 root root   4096 11-12 19:32 test3</div><div class="line">drwxrwxrwx 2 root root   4096 11-12 19:32 test4</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># find . -name "*.log" -mtime +5 -ok rm &#123;&#125; \;</span></div><div class="line">&lt; rm ... ./log_link.log &gt; ? y</div><div class="line">&lt; rm ... ./log2012.log &gt; ? n</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">总计 312</div><div class="line">-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log</div><div class="line">drwxr-xr-x 6 root root   4096 10-27 01:58 scf</div><div class="line">drwxrwxrwx 2 root root   4096 11-12 19:32 test3</div><div class="line">drwxrwxrwx 2 root root   4096 11-12 19:32 test4</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;在上例中，find 命令在当前目录中查找所有文件名以 .log 结尾、更改时间在5日以上的文件，并删除它们，只不过在删除之前先给出提示。按 y 键删除文件，按 n 键不删除。</p>
<h3 id="实例4：-exec-中使用-grep-命令"><a href="#实例4：-exec-中使用-grep-命令" class="headerlink" title="实例4：-exec 中使用 grep  命令"></a>实例4：-exec 中使用 grep  命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">find /etc -name <span class="string">"passwd*"</span> -<span class="built_in">exec</span> grep <span class="string">"root"</span> &#123;&#125; \;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># find /etc -name "passwd*" -exec grep "root" &#123;&#125; \;</span></div><div class="line">root:x:0:0:root:/root:/bin/bash</div><div class="line">root:x:0:0:root:/root:/bin/bash</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>说明</strong></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;任何形式的命令都可以在 -exec 选项中使用。在上例中使用 grep 命令。find 命令首先匹配所有文件名为 “passwd” 的文件，例如：passwd、passwd.old、paaswd.bak，然后执行 grep 命令看看在这些文件中是否存在一个 root 用户。</p>
<h3 id="实例5：查找文件移动到指定目录"><a href="#实例5：查找文件移动到指定目录" class="headerlink" title="实例5：查找文件移动到指定目录"></a>实例5：查找文件移动到指定目录</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">find . -name <span class="string">"*.log"</span> -<span class="built_in">exec</span> mv &#123;&#125; .. \;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div></pre></td><td class="code"><pre><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">总计 12drwxr-xr-x 6 root root 4096 10-27 01:58 scf</div><div class="line">drwxrwxr-x 2 root root 4096 11-12 22:49 test3</div><div class="line">drwxrwxr-x 2 root root 4096 11-12 19:32 test4</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cd test3/</span></div><div class="line">[root@localhost test3]<span class="comment"># ll</span></div><div class="line">总计 304</div><div class="line">-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log</div><div class="line">-rw-r--r-- 1 root root     61 11-12 22:44 log2013.log</div><div class="line">-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log</div><div class="line">[root@localhost test3]<span class="comment"># find . -name "*.log" -exec mv &#123;&#125; .. \;</span></div><div class="line">[root@localhost test3]<span class="comment"># ll</span></div><div class="line">总计 0[root@localhost test3]<span class="comment"># cd ..</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">总计 316</div><div class="line">-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log</div><div class="line">-rw-r--r-- 1 root root     61 11-12 22:44 log2013.log</div><div class="line">-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log</div><div class="line">drwxr-xr-x 6 root root   4096 10-27 01:58 scf</div><div class="line">drwxrwxr-x 2 root root   4096 11-12 22:50 test3</div><div class="line">drwxrwxr-x 2 root root   4096 11-12 19:32 test4</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment">#</span></div></pre></td></tr></table></figure>
<h3 id="实例6：用-exec-选项执行-cp-命令"><a href="#实例6：用-exec-选项执行-cp-命令" class="headerlink" title="实例6：用 exec 选项执行 cp 命令"></a>实例6：用 exec 选项执行 cp 命令</h3><p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>命令</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div></pre></td><td class="code"><pre><div class="line">find . -name <span class="string">"*.log"</span> -<span class="built_in">exec</span> cp &#123;&#125; test3 \;</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;<strong>输出</strong></p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div></pre></td><td class="code"><pre><div class="line">[root@localhost test3]<span class="comment"># ll</span></div><div class="line">总计 0[root@localhost test3]<span class="comment"># cd ..</span></div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># ll</span></div><div class="line">总计 316</div><div class="line">-rw-r--r-- 1 root root 302108 11-03 06:19 log2012.log</div><div class="line">-rw-r--r-- 1 root root     61 11-12 22:44 log2013.log</div><div class="line">-rw-r--r-- 1 root root      0 11-12 22:25 log2014.log</div><div class="line">drwxr-xr-x 6 root root   4096 10-27 01:58 scf</div><div class="line">drwxrwxr-x 2 root root   4096 11-12 22:50 test3</div><div class="line">drwxrwxr-x 2 root root   4096 11-12 19:32 test4</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># find . -name "*.log" -exec cp &#123;&#125; test3 \;</span></div><div class="line">cp: “./test3/log2014.log” 及 “test3/log2014.log” 为同一文件</div><div class="line">cp: “./test3/log2013.log” 及 “test3/log2013.log” 为同一文件</div><div class="line">cp: “./test3/log2012.log” 及 “test3/log2012.log” 为同一文件</div><div class="line">[root@localhost <span class="built_in">test</span>]<span class="comment"># cd test3</span></div><div class="line">[root@localhost test3]<span class="comment"># ll</span></div><div class="line">总计 304</div><div class="line">-rw-r--r-- 1 root root 302108 11-12 22:54 log2012.log</div><div class="line">-rw-r--r-- 1 root root     61 11-12 22:54 log2013.log</div><div class="line">-rw-r--r-- 1 root root      0 11-12 22:54 log2014.log</div><div class="line">[root@localhost test3]<span class="comment">#</span></div></pre></td></tr></table></figure>

	

	
		<span class="different-posts"><a href="/2017/10/12/2. Linux 命令/20. find 命令之 exec/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
